package com.zkca.xf.ui.activity.personprotect;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zkca.xf.Constants_SP;
import com.zkca.xf.MyApplication;
import com.zkca.xf.R;
import com.zkca.xf.base.BaseTitleActivity;
import com.zkca.xf.model.javabean.TaskLBean;
import com.zkca.xf.model.javabean.uploadbean.RequestInspect;
import com.zkca.xf.presenter.Task_LPresenter;
import com.zkca.xf.ui.activity.technicalprotect.Dianhu_LActivity;
import com.zkca.xf.ui.adapter.SingleAdapter;
import com.zkca.xf.ui.adapter.ViewHolder;
import com.zkca.xf.utils.DisplayUtil;
import com.zkca.xf.utils.PopWindowUtil;
import com.zkca.xf.utils.SPUtil;
import com.zkca.xf.utils.SpaceItemDecoration;
import com.zkca.xf.utils.TimeUtils;
import com.zkca.xf.view.TaskLView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TaskLActivity extends BaseTitleActivity implements TaskLView, View.OnClickListener {

    @BindView(R.id.xrv_task)
    XRecyclerView xrvTask;
    @BindView(R.id.wushuju)
    ImageView wushuju;
    @BindView(R.id.xre_dtype)
    XRecyclerView xreDtype;
    @BindView(R.id.shaixuan_begantime)
    TextView shaixuanBegantime;
    @BindView(R.id.shaixuanbeganImage)
    ImageView shaixuanbeganImage;
    @BindView(R.id.bgtime)
    RelativeLayout bgtime;
    @BindView(R.id.shaixuan_overtime)
    TextView shaixuanOvertime;
    @BindView(R.id.shaixuan_overimage)
    ImageView shaixuanOverimage;
    @BindView(R.id.ovtime)
    RelativeLayout ovtime;
    @BindView(R.id.shaixuan_cancel)
    TextView shaixuanCancel;
    @BindView(R.id.shaixuan_sure)
    TextView shaixuanSure;
    @BindView(R.id.drawerLayout)
    DrawerLayout drawerLayout;

    private Task_LPresenter task_lPresenter;
    private RequestInspect requestInspect;
    private LinearLayoutManager linearLayoutManager;
    int pageNum = 1;
    int pageSize = 10;
    private List<TaskLBean.DataBean> dataBeanList = new ArrayList<>();
    private SingleAdapter<TaskLBean.DataBean> adapter;
    private ImageView imgRight;
    View pop_selection_state;
    private TextView tv_pop_1, tv_pop_2, tv_pop_3;
    private int taskStatus=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_l);
        ButterKnife.bind(this);
        task_lPresenter=new Task_LPresenter(this,this);
        initAdapter();
        initMyview();


    }

    private void initAdapter() {
        adapter=new SingleAdapter<TaskLBean.DataBean>(mContext,dataBeanList,R.layout.task_item) {
            @Override
            public void convert(ViewHolder holder, final TaskLBean.DataBean dataBean, int position) {
                TextView taskstatus = holder.getView(R.id.taskstaues);//任务状态
                TextView tasknum = holder.getView(R.id.tasknum);//任务编号
                TextView taskname = holder.getView(R.id.taskname);//任务名称
                TextView begantime = holder.getView(R.id.begantime);//任务开始时间
                TextView overtime = holder.getView(R.id.overtime);//任务结束时间
                TextView taskuser = holder.getView(R.id.taskuser);//任务执行人
                ImageView imageView = holder.getView(R.id.tatil_back);


                int taskStatus = dataBean.getTaskStatus();
                final String taskNo = (String) dataBean.getTaskNo();//巡检ID
                String taskName = dataBean.getTaskName();//巡检名称
                long startTime = dataBean.getStartTime();//开始时间
                long endTime = dataBean.getEndTime();//结束时间
                String userName = dataBean.getUserName();//任务执行人
                String starttime = TimeUtils.getFormatDate2(startTime);
                String endtime = TimeUtils.getFormatDate2(endTime);
                final int taskId = dataBean.getTaskId();

                tasknum.setText(taskNo+"");
                taskname.setText(taskName);
                begantime.setText(starttime);
                overtime.setText(endtime);
                taskuser.setText(userName);
                if (taskStatus==0){
                    imageView.setImageResource(R.mipmap.hong_title);
                    taskstatus.setText("待执行");
                    taskstatus.setTextColor(Color.rgb(241, 90, 74));
                }else if (1==taskStatus){
                    imageView.setImageResource(R.mipmap.lv_title);
                    taskstatus.setText("执行中");
                    taskstatus.setTextColor(Color.rgb(41, 171, 145));
                }else if (2==taskStatus){
                    imageView.setImageResource(R.mipmap.hui_title);
                    taskstatus.setText("已过期");
                    taskstatus.setTextColor(Color.rgb(153, 153, 153));
                }
                holder.setItemViewOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent();
                        intent.putExtra("taskId",taskId);
                        intent.putExtra("taskStudes",dataBean.getTaskStatus());
                        intent.setClass(TaskLActivity.this,Job_LActivity.class);
                        startActivity(intent);
                    }
                });
            }
        };
    }

    private void initMyview() {
        setTitleText("任务列表");
        setleftTextVisibility(true);
        setleftImgVisibility(true);
        setImgVisibility(true);
        initDvData(pageNum ,taskStatus);

        imgRight = getImgRight();
        imgRight.setOnClickListener(this);
        pop_selection_state = LayoutInflater.from(this).inflate(R.layout.pop_taskstatues, null, false);
        tv_pop_1 = pop_selection_state.findViewById(R.id.tv_pop_1);
        tv_pop_2 = pop_selection_state.findViewById(R.id.tv_pop_2);
        tv_pop_3 = pop_selection_state.findViewById(R.id.tv_pop_3);

        tv_pop_1.setOnClickListener(this);
        tv_pop_2.setOnClickListener(this);
        tv_pop_3.setOnClickListener(this);



        linearLayoutManager = new LinearLayoutManager(mContext);
        xrvTask.setLayoutManager(linearLayoutManager);
        xrvTask.addItemDecoration(new SpaceItemDecoration(DisplayUtil.dip2px(
                mContext, 0.5f)));
        xrvTask.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                xrvTask.setLoadingMoreEnabled(true);
                dataBeanList.clear();
                pageNum=1;
                initDvData(pageNum ,taskStatus);
            }

            @Override
            public void onLoadMore() {
                pageNum++;
                initDvData(pageNum ,taskStatus);
            }
        });
        xrvTask.setAdapter(adapter);
    }


    private void initDvData(int pagenum,int statues) {
        Map map = new HashMap<String, Object>();
        map.put("taskStatus",statues);
        requestInspect = new RequestInspect();
        requestInspect.setParam(map);
        requestInspect.setPageNum(pagenum);
        requestInspect.setPageSize(pageSize);
        requestInspect.setAccessId(SPUtil.getString(this, Constants_SP.ACCESSID));
        requestInspect.setAction(3);
        requestInspect.setTimeStamp(TimeUtils.getTime());
        requestInspect.setSign(MyApplication.getInstance().generateDvSign(requestInspect));
        task_lPresenter.getTaskL(requestInspect);
    }

    @Override
    public void showProgressDialog() {

    }

    @Override
    public void hideProgressDialog() {

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void getTaskL(TaskLBean taskLBean) {
        xrvTask.refreshComplete();
        xrvTask.loadMoreComplete();
        if (taskLBean.getResult() == 0) {
            List<TaskLBean.DataBean> data = taskLBean.getData();
            dataBeanList.addAll(data);

            if (taskLBean.getTotalpage() == pageNum) {
                xrvTask.setLoadingMoreEnabled(false);
            }
            if (pageNum == 1 && data.size() == 0) {
                wushuju.setVisibility(View.VISIBLE);
            } else {
                wushuju.setVisibility(View.GONE);
            }
            adapter.notifyDataSetChanged();

        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.im_base_right:
                PopWindowUtil.taskState(TaskLActivity.this, pop_selection_state, imgRight);
                break;
            case R.id.tv_pop_1:
                taskStatus = 0;
                dataBeanList.clear();
                initDvData(pageNum ,taskStatus);
                PopWindowUtil.popupWindow.dismiss();
                break;
            case R.id.tv_pop_2:
                taskStatus = 1;
                dataBeanList.clear();
                initDvData(pageNum ,taskStatus);
                PopWindowUtil.popupWindow.dismiss();
                break;
            case R.id.tv_pop_3:
                taskStatus = 2;
                dataBeanList.clear();
                initDvData(pageNum ,taskStatus);
                PopWindowUtil.popupWindow.dismiss();
                break;

        }
    }
}
